package org.blitengine.layers
{
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.geom.Point;
	
	import org.blitengine.effects.IEffect;
	import org.blitengine.effects.NullEffect;
	import org.blitengine.objects.BlitObject;

	public class Layer implements ILayer
	{
		public var objects:Array = [];
		public var paralaxFactor:Number = 1;
		public var bitmapData:BitmapData;
		public var effect:IEffect = new NullEffect();
		
		public function Layer( paralaxFactor:int = 1 )
		{
			bitmapData = new BitmapData(32, 32, true, 0x0);
		}

		public function setSize(width:int, heigth:int):void
		{
			bitmapData = new BitmapData(width, heigth, true, 0x0);
		}

		public function add(object:BlitObject):void
		{
			objects.push(object);
		}
		
		public function update(cameraX:Number, cameraY:Number):void
		{
			if(effect != null)
				effect.beforeRender(bitmapData);
			
			updateLayer(cameraX, cameraY);
			
			if(effect!=null)
				effect.afterRender(bitmapData);
		}
		
		public function get output():IBitmapDrawable
		{
			return bitmapData;
		}
		
		/**
		 * Override this function in subclasses.
		 * @see TileMapLayer
		 * */
		protected function updateLayer(cameraX:Number, cameraY:Number):void
		{
			var o:BlitObject;
			var n:int = objects.length;
			var w2:int = bitmapData.width/2, h2:int = bitmapData.height/2;
			for(var i:int=0; i<n; i++)
			{
				o = BlitObject(objects[i]);
				o.blit(bitmapData, new Point(o.x-cameraX+w2, o.y-cameraY+h2));
			}
		}
		
	}
}